<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        .box {
            width: 350px;
            height: 430px;
            border: 1px solid #000;
            margin: 200px;
            position: relative;
        }

        .top {
            width: 350px;
            height: 350px;
            border-bottom: 1px solid #000;
            position: relative;
        }

        .top img {
            width: 350px;
            height: 350px;
        }

        .small {
            width: 175px;
            height: 175px;
            background-color: rgba(0, 0, 0, .3);
            position: absolute;
            left: 0;
            top: 0;
            cursor: move;
            display: none;

        }

        ul {
            height: 80px;
            display: flex;
            justify-content: space-around;
            align-items: center;
        }

        li {
            list-style: none;
            width: 60px;
            height: 60px;
        }

        li:hover {
            border: 1px solid skyblue;
        }

        li img {
            width: 60px;
            height: 60px;
            border: 1px solid #000;
        }

        #big {
            width: 400px;
            height: 400px;
            position: absolute;
            right: -410px;
            top: 0;
            border: 1px solid #000;
            overflow: hidden;
            display: none;
        }

        #big img {
            position: absolute;
            left: 0;
            top: 0;
        }
    </style>
</head>

<body>
    <div class="box">
        <div class="top">
            <img src="001.jpg" alt="" id='smallimg'>
            <div class="small"></div>
        </div>
        <div class="bottom">
            <ul>
                <li><a href="0001.jpg"><img src="001.jpg" alt=""></a> </li>
                <li><a href="l5.jpg"><img src="l5.jpg" alt=""></a> </li>
                <li><a href="0001.jpg"><img src="001.jpg" alt=""></a> </li>
                <li><a href="0001.jpg"><img src="001.jpg" alt=""></a> </li>
                <li><a href="0001.jpg"><img src="001.jpg" alt=""></a> </li>
            </ul>
        </div>
        <div id="big"><img src="0001.jpg" alt="" id="bigimg"></div>
    </div>
    <script>
        /*
       1.分析需求(交互)：
       1,点击小图片,上面出现对应的大图片,默认为选中第一张
       2,鼠标移入上面的图片,在图里面出现小方框,小方框置于鼠标中间,且不能超过边框,
       在右边出现大方块和放大的图片
       */
        //获取元素
        var box = document.getElementsByClassName('box')[0];
        var liList = document.getElementsByTagName('li');
        var top1 = document.getElementsByClassName('top')[0];
        var small = document.getElementsByClassName('small')[0];
        var smallimg = document.getElementById('smallimg');
        var big = document.getElementById('big');
        var bigimg = document.getElementById('bigimg');

        //注册事件
        for (var i = 0; i < liList.length; i++) {
            var li = liList[i];
            li.onclick = function () {
                smallimg.src = this.firstElementChild.firstElementChild.src
                bigimg.src = this.firstElementChild.href
                return false;
            }
        }
        top1.onmouseover = function () {
            small.style.display = 'block';
            big.style.display = 'block';
        }
        top1.onmouseout = function () {
            small.style.display = 'none';
            big.style.display = 'none';
        }
        top1.onmousemove = function (e) {
            e = e || window.event;
            var x = e.pageX - box.offsetLeft;
            var y = e.pageY - box.offsetTop;
            x =  x-small.offsetWidth / 2;
            y =  y-small.offsetHeight / 2;
            x = x <= 0 ? 0 : x;
            x = x >= 175?175:x;
            y = y <= 0 ? 0 : y;
            y = y >= 175?175:y;
            console.log(x);
            console.log(y);

            small.style.left = x + 'px';
            small.style.top = y + 'px';
            bigimg.style.left = -bigimg.offsetWidth/top1.offsetWidth * small.offsetLeft + 'px';
            bigimg.style.top = -bigimg.offsetHeight/top1.offsetHeight * small.offsetTop + 'px';
            console.log(bigimg.style.left);
            

        }
    </script>
</body>

</html>